<template>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" style="margin:10px 10px;">
  <el-form-item label="用户名：" prop="name">
     <el-input v-model="ruleForm.name"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="手机号：" prop="mobile">
     <el-input v-model="ruleForm.mobile"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="真实姓名：">
     <el-input v-model="ruleForm.truename"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="积分：" prop="integral">
	 <el-input v-model="ruleForm.integral"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="余额：" prop="balance">
	 <el-input v-model="ruleForm.balance"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</template>
<script>

  export default {
    data() {
	  const validNumCheck = (rule, value, callback) => {
			   
			if (isNaN(value)) {
				callback(new Error('请输入数字'));
			}  else {
				callback();
			}
	  };
	  const validPhoneCheck = (rule, value, callback) => {
			var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;  

			if (!myreg.test(value)) {
				callback(new Error('请注意手机格式'));
			}  else {
				callback();
			}
	  };
      return {
        ruleForm: {},
		rules: {
          name: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
          ],
		  mobile: [
			{ required: true, message: '请输入手机号', trigger: 'blur' },
			{ required: true, validator: validPhoneCheck, trigger: 'blur' }
		  ],
		  integral: [
			{ required: true, message: '请输入积分', trigger: 'blur' },
			{ required: true, validator: validNumCheck, trigger: 'blur' }
		  ],
		  balance: [
			{ required: true, message: '请输入余额', trigger: 'blur' },
			{ required: true, validator: validNumCheck, trigger: 'blur' }
		  ]
        }
      };
    },
    methods: {
	  submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios({
				method:"post",
				url:this.host+"/admin/index.php/Distributor/saveDistributor",
				data:{
					distributor_id : this.$route.query.distributor_id,
					name: this.ruleForm.name,
					mobile: this.ruleForm.mobile,
					truename: this.ruleForm.truename,
					balance: this.ruleForm.balance,
					integral: this.ruleForm.integral,
				}
			}).then((res)=>{
				this.$message({
				  message: '保存成功',
				  type: 'success'
				});
				this.$router.push({ path: 'Distributor-list' })
			})
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
	  resetForm(formName) {
        this.$refs[formName].resetFields();
      },
	  getDistributor(){
		this.$axios({
			method:"post",
			url:this.host+"/admin/index.php/Distributor/getDistributor",
			data:{
				distributor_id : this.$route.query.distributor_id
			}
		}).then((res)=>{
			this.ruleForm = res.data 
		})
	  }
    },
	mounted () {
		if(this.$route.query.distributor_id)
			this.getDistributor()	
	}
  }
</script>